let C = require('../../utils/wxpage').C
let utils = require('../../utils/utils');
let time1;
let time2;
module.exports = C('specifications', function (vm) {
    return {
        data: {
            selectedData:[],
            pNum:0,
        },
        onLoad: function (options) {
            clearInterval(time1);
            //console.log('商品属性 组件调用');
            var that=this;
            var finData=wx.getSystemInfoSync();
            //console.log('规格列表的onLoad');
            //console.log(finData);
            vm.$set({
                setHeight:finData.windowHeight*0.8
            })
            //console.log(vm.$data().setHeight);
            //console.log('specifications  ---------------------------- onLoad');
            //console.log(this.data.selectData);
            let time=setTimeout(function(){
                clearInterval(time2);
                 time1=setInterval(function(){
                    //console.log('定时器 1');
                    if(that.data.selectData){
                        //console.log('有数据进来组件了---------------------------------------------------------');
                        //console.log(that.data.selectData);
                        that.setPrice();
                        clearInterval(time1);
                    }
                },1000);
            });

        },
        onReady: function () {
            //console.log('specifications  ------------------ onReady');
            //console.log(this.data.selectData);
        },
        onShow: function () {
            //console.log('specifications  ----------------------------- onShow');
            //console.log(this.data.selectData);
        },
        //选择规格
        selected:function(e){
            //console.log('选择规格----------------------------------------------------------------------------------');
            //清除默认设置
            let isRepeat=false; //是否为重复选项
            let parId=e.currentTarget.dataset.par;
            let maxCount=e.currentTarget.dataset.choose;
            let specifData=this.data.specifData;//商品数据///////////////////////////////////////////////////
            //console.log('商品数据');
            //console.log(specifData);
            //console.log('组件里的 选择规格');
            //console.log(e);
            // ////console.log(this);
            if(vm.$data().sel==undefined){
                //console.log('未设置动态的规格选项');
                //清除之前的固定数据
                let setData=[];
                //console.log('清之前的固定数据');
                this.data.specifData.property.forEach(function(value,index,arr){
                    //console.log(value);
                    value.property[0].isSel=false;
                    //console.log(index);

                });
                let sel=this.data.specifData.property;

                vm.$set({
                    sel
                });
                this.clearIndexSel(parId);
            }else{
                // //////console.log('已设置动态规格数据选项');
                let sel=vm.$data().sel;
                //清除选择数据
                //console.log('查看清除之前的选择数据');
                //console.log(vm.$data().selectedData);
                this.clearIndexSel(parId);
                //////console.log(sel);
            }
            //console.log('11111111111111111111111111111111111111111111111111已有的数据');
            //console.log(vm.$data().selectedData);
            //console.log(vm.$data().sel);
            //////console.log(e);

            let indexs=e.currentTarget.id;
            let selectedData= vm.$data().selectedData;
            let sel=vm.$data().sel;
            //是否有重复的选项
            if(selectedData.length>0) {
                //////console.log('拥有设置数据');
                selectedData.forEach(function (value, index, arr) {
                    value.indexs.forEach(function (val, ind, Arr) {
                        if(index==parId&&val.i==indexs){
                            //////console.log('已有重复数据');
                            isRepeat=true;
                            selectedData[index].indexs.splice(ind,1);
                            //////console.log(val);
                            //////console.log('删除后');
                            //////console.log(selectedData);
                            sel[parId].property[indexs].isSel=false;
                            //////console.log('动态数据修改后');
                            //////console.log(sel);
                            vm.$set({
                                sel,
                                selectedData
                            })
                        }else{
                            //////console.log('没有重复数据');
                        }
                    });
                });
            }
            if(selectedData[parId]==undefined){
                selectedData[parId]={};
            }
            if(selectedData[parId].maxCount==undefined){
                //////console.log('maxCount 未定义');
                selectedData[parId].maxCount=maxCount;
            }
            if(selectedData[parId].indexs==undefined){
                //////console.log('选定数据');
                selectedData[parId].indexs=[];
            }
            if(selectedData[parId].indexs.length<selectedData[parId].maxCount&&!isRepeat){
                //console.log('还能选  oneid----------------------------------------------------');
                // //console.log(sel);
               let oneid=specifData.property[parId].p_k;
                //console.log(oneid);
                var obj={
                    i:indexs,
                    val:sel[parId].property[indexs].val,
                    oneid
                }
                selectedData[parId].indexs[selectedData[parId].indexs.length]=obj;
                vm.$set({
                    selectedData
                });
                sel.forEach(function(value,index,arr){
                    value.property.forEach(function(val,ind,arr2){
                        //////console.log(arr2);
                        //////console.log(val);

                        if(selectedData[index]){
                            //////console.log('拥有设置数据');
                            selectedData[index].indexs.forEach(function(dVal,dInd,dArr2){
                                //动态数据里保存的选项下标
                                //固定数据的下标
                                //////console.log(ind);
                                if(dVal.i==ind){
                                    //////console.log('配对');
                                    val.isSel=true;
                                    //////console.log(val);
                                }else{
                                }
                            });
                        }
                    })
                });
                //console.log('修改后的数据');
                //console.log(sel);
                //console.log(selectedData);
                vm.$set({
                    sel
                });
                //////console.log('循环结束');
            }else{
                //////console.log('已达最大数量 或者 是重复元素');
                //////console.log(isRepeat);
            }
            // 根据规格修改价格 可在已达最大数量时不调用  进行优化 根据规格拼接带属性的商品id
            this.setPrice();


        },
        //根据规格修改价格  根据规格拼接带属性的商品id
        setPrice:function(){
            //根据规格拼接带属性的商品id
            let pId=this.data.specifData.id;
            let specifData=this.data.specifData;
            //console.log(pId);
            //////console.log('商品id');
            //////console.log(pId);
            //console.log('根据规格修改价格------------------------------------------------');
            let addPrice=0;
            if(vm.$data().selectedData.length>0){
                //console.log('使用正常值');
                // selectedData= vm.$data().selectedData;
                //console.log(vm.$data().selectedData);
            }else{
                //console.log('使用默认值-----------------------------------------------');
                //直接替换
                vm.$data().selectedData=this.data.selectData;
            }

            let selectedData= vm.$data().selectedData;
            //console.log('规格数据--------------------------------');
            //console.log(selectedData);
            //////console.log(selectedData);
            //"allid": "419492:0-0:0-1:1-0",
            //419492:0-0:0-1:1-0
            console.log(specifData.property);
            selectedData.forEach(function(value,index,arr){
                //console.log('外层循化');
                //////console.log(value);
                value.indexs.forEach(function(val,ind,Arr){
                    if(val.oneid==undefined){
                        console.log('空值');
                        pId+=':'+specifData.property[index].p_k+'-'+val.i;
                    }else{
                        pId+=':'+val.oneid+'-'+val.i;
                    }
                    //console.log(val.val);
                    addPrice+=parseFloat(val.val);
                })
            });
            //console.log('价格计算 与pid拼接 完成');
            //console.log(addPrice);
            //console.log(pId);
            //拿到显示信息
            //////console.log(this.data.specifData);
            //console.log(addPrice);
            //console.log(this.data.specifData.jg);
            let prices=addPrice+parseFloat(this.data.specifData.jg);
            console.log('此处拼接pId------------------------------------');
            console.log(pId);
            vm.$set({
                setPrice:prices,
                pId
            });
            //console.log('属性id拼接完成 ---------------------------------');
            //console.log(vm.$data().pId);
            //console.log(vm.$data().setPrice);
            ////console.log('查找商品属性的数据');
            let obj={
                pIdY:this.data.specifData.id,
                pId,
                success:this.pNumSuccess
            };
            ////console.log(this.data.specifData.id);
            ////console.log(pId);
            let pNum=utils.getCart(this,'',obj);
            ////console.log('返回该属性的数量成功');
            ////console.log(pNum);
            //utils.selectPNum(this.data.specifData.id,pId);
        },
        //添加减少商品
        operating:function(e){
            ////console.log('属性页的减少商品');
            if(vm.$data().pNum==0){
                utils.modal('提示','没有数量');
            }else{
                ////console.log('规格的 添加减少商品');
                //////console.log('组件中的加减购物车');
                var shop = e.currentTarget.dataset.shopid;//商铺id
                if(vm.$data().pId){
                    var p=vm.$data().pId;
                    ////console.log('组件的 商品减少-----------------');
                    ////console.log(e);
                    ////console.log(this);
                    ////console.log(p);
                    utils.operating(e,this,p);
                    vm.$set({
                        pNum:vm.$data().pNum-1
                    })
                }else{
                    utils.modal('提示','还没选好属性呢');
                }
            }
        },
        //加入购物车
        addCart:function(e){
            let pId=vm.$data().pId;
            let arrData=[];
            console.log('规格组件加入购物车');
            //console.log(this.data);
            if(pId){
                //console.log('使用正常值加入购物车');
                ////console.log('');
                var p=vm.$data().pId;
                //console.log(p);
                utils.addCart(e,this,p,vm);
                ////console.log('购物车该属性商品数量');
                ////console.log(vm.$data().pNum);
                vm.$set({
                    pNum:vm.$data().pNum+1
                })

            }else{

                if(this.data.selectData){
                    //console.log('使用默认值设置参数');
                    this.setPrice();
                    // e,t_this,specif,
                    //console.log(vm.$data().pId);
                    utils.addCart(e,this,vm.$data().pId,vm);
                }else{
                    utils.modal('提示','商品规格还没选好呢');
                }

            }
            console.log(pId);
             console.log(pId.indexOf('undefined'));

        },
        //根据 属性 PId 查找购物车数量成功的回调函数
        pNumSuccess:function(num){
            //console.log('查找购物车数量成功的回调函数');
            //console.log(num);
            vm.$set({
                pNum:num
            });
            ////console.log('购物车数据');
            ////console.log(this.data.cartDataList);
        },
        //隐藏规格功能
        specifHidden:function(){
            this.resetData();
        },
        //隐藏规格重置数据
        resetData:function(){
            let that=this;
            let sel=vm.$data().selectedData;
            that.data.selectData=undefined;
            ////console.log(sel);
            ////console.log('隐藏规格重置数据');
            vm.$set({
                pNum:0,
                selectedData:[],
                sel:null,
                setPrice:0,
                pId:undefined

            });
            ////console.log('父组件的模型数据');
            ////console.log(this.data.specifShow);
            ////console.log(this.data.selectedData);
            this.setData({
                specifShow:false,
                selectedData:null
            });
            time2=setInterval(function(){
                clearInterval(time1);
                //console.log('定时器 time2');
                if(that.data.selectData){
                    //console.log('有数据进来组件了---------------------------------------------------------');
                    //console.log(that.data.selectData);
                    that.setPrice();
                    clearInterval(time2);
                }
            },1000);
        },
        //如果未选择属性自己设置默认属性
        setDefault:function(){
            //console.log("如果未选择属性自己设置默认属性");
        },
        //查看当前类是否有规格数据  清除当前类的设置
        clearIndexSel:function(index){
        //console.log('点击的是'+index+'类');
        let selectedData=vm.$data().selectedData;
        let sel=vm.$data().sel;
        //console.log('在清除方法中查看当前类的设置');
        //console.log(sel);
        //console.log(selectedData);
        //console.log('清除当前类数据');
        //console.log(selectedData[index]);
        //清除选中数据
            console.log('bug----------');
            console.log(selectedData);
        selectedData[index].indexs=[];
        // selectedData.forEach(function(value,index,arr){
        //     //console.log('循化清除');
        //     //console.log(value);
        //     value.indexs=[];
        // });
        //清除显示数据
        sel[index].property.forEach(function(value,index,arr){
          value.isSel=false;
        });
        // sel.forEach(function(value,index,arr){
        //   //console.log(value);
        //   value.property.forEach(function(val,ind,Arr){
        //       //console.log(val);
        //       val.isSel=false;
        //   })
        // });
        vm.$set({
            selectedData,
            sel
        });
            //console.log('清除后的操作数据');
        //console.log(selectedData);
            //console.log('保存的数据');
        //console.log(vm.$data().selectedData);
    }
    }

})
